<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="产品编号" min-width="150" v-if="false">
        <template #default="{ row }">
         
          {{row.productId}}
        </template>
      </el-table-column>
      <el-table-column label="产品名称" min-width="150">
        <template #default="{ row}">

            {{row.productName}}
        </template>
      </el-table-column>
      <el-table-column label="产品单位" min-width="150">
        <template #default="{ row }">
         
          {{row.productUnitName}}
        </template>
      </el-table-column>
      <el-table-column label="产品规格" min-width="150">
        <template #default="{ row }">
          <!-- <el-form-item :prop="`${$index}.productStandards`" :rules="formRules.productStandards" class="mb-0px!">
            <el-input v-model="row.productStandards" placeholder="请输入产品规格" />
          </el-form-item> -->
          {{row.productStandards}}
        </template>
      </el-table-column>
      <el-table-column label="产品单价" min-width="150">
        <template #default="{ row }">
          <!-- <el-form-item :prop="`${$index}.productPrice`" :rules="formRules.productPrice" class="mb-0px!">
            <el-input v-model="row.productPrice" placeholder="请输入产品单价" />
          </el-form-item> -->
          {{row.productPrice}}
        </template>
      </el-table-column>
      <el-table-column label="数量" min-width="150">
        <template #default="{ row}">
          <!-- <el-form-item :prop="`${$index}.count`" :rules="formRules.count" class="mb-0px!">
            <el-input v-model="row.count" placeholder="请输入数量" />
          </el-form-item> -->
          {{row.count}}
        </template>
      </el-table-column>
      <el-table-column label="批次" min-width="150">
        <template #default="{ row }">
          <!-- <el-form-item :prop="`${$index}.batch`" :rules="formRules.batch" class="mb-0px!">
            <el-input v-model="row.batch" placeholder="请输入批次" />
          </el-form-item> -->
          {{row.batch}}
        </template>
      </el-table-column>
      <el-table-column label="有效期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.validityDate`" :rules="formRules.validityDate" class="mb-0px!">
            <el-date-picker
              v-model="row.validityDate"
              type="date"
              value-format="x"
              placeholder="选择有效期"
              :disabled="true"
            />
          </el-form-item>
        </template>
      </el-table-column>
     
      <el-table-column label="总价" min-width="150" v-if="false">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.totalPrice`" :rules="formRules.totalPrice" class="mb-0px!">
            <el-input v-model="row.totalPrice" placeholder="请输入总价" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row }">
          <!-- <el-form-item :prop="`${$index}.remark`"  class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item> -->
          {{row.remark}}
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column> -->
    </el-table>
  </el-form>
  <!-- <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加单项</el-button>
  </el-row> -->
</template>
<script setup lang="ts">
import { SupplierSendOrderApi } from '@/api/erp/suppliersendorder'

const props = defineProps<{
  outOrderId: undefined, // 随货同行单编号（主表的关联字段）
  purchaseOrderId:undefined

}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formData1 = ref([])
const formData2 = ref([])
const formRules = reactive({
  outOrderId: [{ required: true, message: '随货同行单编号不能为空', trigger: 'blur' }],
  productId: [{ required: true, message: '产品编号不能为空', trigger: 'blur' }],
  productUnit: [{ required: true, message: '产品单位不能为空', trigger: 'blur' }],
  productStandards: [{ required: true, message: '产品规格不能为空', trigger: 'blur' }],
  productPrice: [{ required: true, message: '产品单价不能为空', trigger: 'blur' }],
  batch: [{ required: true, message: '批次不能为空', trigger: 'blur' }],
  validityDate: [{ required: true, message: '有效期不能为空', trigger: 'blur' }],
  count: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
  totalPrice: [{ required: true, message: '总价不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.outOrderId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    formData1.value = []
    formData2.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
     
      formData1.value = await SupplierSendOrderApi.getSupplierSendOrderItemListByPurchaseOrderId(props.purchaseOrderId)
                                                  
      formData.value = await SupplierSendOrderApi.getSupplierSendOrderItemListByOutOrderId(val)

      if(formData1.value !== null){
         for(let i = 0; i < formData1.value.length; i++){
          const dict ={
            value: undefined,
            label:undefined,
          }
          dict.label = formData1.value[i].productName
          dict.value = formData1.value[i].productId
          formData2.value.push(dict)
         }

      }
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

const onChange = (name,index)=>{
  formData1.value.forEach(element => {
    if(name === element.productId) {
      const row = {
        productId:element.productId,
        productName:element.productName,
        productUnit:element.productUnit,
        productStandards:element.productStandards,
        productPrice:element.productPrice,
      }
      if (index >= 0 && index < formData.value.length) {
        formData.value.splice(index, 1, row);
      }
    }
  });
}
/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    outOrderId: undefined,
    productId: undefined,
    productUnit: undefined,
    productStandards: undefined,
    productPrice: undefined,
    batch: undefined,
    validityDate: undefined,
    count: undefined,
    totalPrice: undefined,
    remark: undefined,
  }
  row.outOrderId = props.outOrderId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>